package com.woniuxy.service.impl;

import com.woniuxy.Exception.serviceException;
import com.woniuxy.dao.DepartmentsDao;
import com.woniuxy.dao.PrincipalDao;
import com.woniuxy.entity.Departments;
import com.woniuxy.entity.PageBean;
import com.woniuxy.service.DepartmentsService;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentsServiceImpl implements DepartmentsService {
    @Autowired
    DepartmentsDao departmentsDao;
    @Autowired
    PrincipalDao principalDao;

    //添加科室
    @Override
    public void addDepartments(Departments departments) {
        departmentsDao.insertDepartments(departments);
    }

    //移除科室
    @Override
    public void removeDepartments(String departmentsId) {
        departmentsDao.deleteDepartments(departmentsId);


    }

    //修改科室信息
    @Override
    public void updateDepartments(Departments departments) {
        departmentsDao.updateDepartments(departments);

    }

    //按条件分页获取科室信息
    @Override
    public PageBean<Departments> getAllDepartments(Departments departments, int pageIndex, int pageSize) {
        try {
            PageBean<Departments> pageBean = new PageBean<>();
            List<Departments> departmentses = departmentsDao.selectDepartments(departments, pageIndex, pageSize);
            for (Departments departments1 : departmentses) {
                departments1.setPrincipal(principalDao.selectPrincipalById(departments1.getPrincipalId()));


            }

            //当前页
            pageBean.setPageIndex(pageIndex);
            //存储页大小
            pageBean.setPageSize(pageSize);

            //获取总条数
            int count = departmentsDao.selectDepartmentsCount(departments);
            pageBean.setTotalRecord(count);
            pageBean.setBeanList(departmentses);
            return pageBean;
        } catch (PersistenceException e) {
            throw new serviceException("系统维护中");
        }
    }

    @Override
    public List<Departments> getAll() {

        ArrayList<Departments> departments = new ArrayList<>();
        List<Departments> deps =  departmentsDao.getAll();
        for (Departments dep:deps){
            departments.add(dep);
        }

        return departments;
    }

}
